<template>
  <web-view
    :webview-styles="webviewStyles"
    :src="src"
    @message="onMessage"
    @error="onError"
  ></web-view>
</template>
<script lang="ts" setup>
import { onLoad } from "@dcloudio/uni-app";
import { ref } from "vue";

const src = ref("");

const webviewStyles = {
  progress: {
    color: "#3c9cff",
  },
};

onLoad((query) => {
  if (!query || !query.src) {
    uni.showToast({
      title: "缺少网页地址",
      icon: "none",
    });
    return;
  }
  src.value = decodeURIComponent(query.src);
});

const onError = () => {
  uni.showToast({
    title: "网页加载失败",
    icon: "none",
  });
  uni.navigateBack();
};

const onMessage = (e: {
  detail: {
    data: {
      type: string;
      data: any;
    };
  };
}) => {
  console.log("onMessage", e);
};
</script>
